package util.mycomponents
{
	import MyApp.View.ValueEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.FlexEvent;
	
	import spark.components.Button;
	import spark.components.HGroup;
	import spark.layouts.VerticalLayout;
	/**
	 * 点击查询或按下enter都会提交动作
	 */ 
	[Event(name="action", type="MyApp.View.ValueEvent")]
	public class MySearchForm extends HGroup
	{
		private var sBtn:Button;
		[Bindable]
		private var sInput:PromptingTextInput;
		[Bindable]
		public var queryString:String;
		[Bindable]
		/**
		 * 提示文本
		 */ 
		public var promt:String="请输入查询内容";
		[Bindable]
		public var showSubmit:Boolean=true;
		
		public function MySearchForm()
		{
			super();
			this.verticalAlign="bottom";
		}
		/**
		 *输入框内的查询文本
		 */ 
		public function set sText(qs:String):void{
			sInput.text=qs;
			this.queryString=qs;
		}
		[Bindable]
		public function get sText():String{
			return sInput.text;
		}
		protected function setQueryString(e:Event):void{
			this.queryString=sInput.text;
		}
		protected function search(e:Event):void{
			this.dispatchEvent(new ValueEvent("action",queryString,true));
		}
		override protected function createChildren():void{
			super.createChildren();
			//button
			if(this.showSubmit){
				sBtn=new Button();
				sBtn.label="查询";
			}
			//input
			sInput=new PromptingTextInput();
			sInput.width=350;
			sInput.height=25;
			sInput.prompt=promt;
			sInput.text=queryString;
			super.addElement(sInput);
			if(this.showSubmit){
				super.addElement(sBtn);
			}
			//添加监听
			if(this.showSubmit){
				sBtn.addEventListener(MouseEvent.CLICK,search);
			}
			sInput.addEventListener(FlexEvent.ENTER,search);
			sInput.addEventListener(Event.CHANGE,setQueryString);
		}
		
	}
}